﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;

public partial class Default3 : System.Web.UI.Page
{
    string strCon = System.Configuration.ConfigurationManager.ConnectionStrings["xxnewsConnectionString"].ConnectionString;
    SqlConnection sqlcon;
    SqlCommand sqlcom;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["pn"] == "" || Session["pn"] == null)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        if (Session["pngeren"] == "" || Session["pngeren"] == null)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        if (Session["pn"].ToString().Contains("设备科") == false)
        {
            Response.Redirect("default.aspx?lb=审批");
        }
        Label1.Text = Session["pn"].ToString();
        Label2.Text = Session["pngeren"].ToString();
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {

            string txt1 = ((TextBox)GridView1.Rows[i].Cells[10].FindControl("TextBox2")).Text;
            if (txt1.Length == 0)
            {
                string jinggao1 = "<SCRIPT language=javascript>alert('";
                jinggao1 += "请输入序号为 " + (GridView1.PageSize * GridView1.PageIndex + i + 1).ToString() + " 的燃油金额！";
                jinggao1 += "');</SCRIPT>";
                ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao1);

                //ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao1, true);
                return;
            }
            for (int j = 0; j < txt1.Trim().Length; j++)
            {
                char a;
                int k;
                a = Convert.ToChar(txt1.Substring(j, 1));
                k = (int)a;
                if (k < 48 || k > 57)
                {
                    string jinggao2 = "<SCRIPT language=javascript>alert('";
                    jinggao2 += "请在序号为 " + (GridView1.PageSize * GridView1.PageIndex + i + 1).ToString() + " 的燃油金额中输入数字！";
                    jinggao2 += "');</SCRIPT>";
                    ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao2);

                    //ScriptManager.RegisterStartupScript(UpdatePanel1, this.GetType(), "", jinggao2, true);

                    return;
                }
            }

        }
        
        
        if (DropDownList_liezhi.SelectedValue=="请选择")
        {
            string jinggao2 = "<SCRIPT language=javascript>alert('";
            jinggao2 += "请选择列支情况！";
            jinggao2 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa2", jinggao2);
            DropDownList_liezhi.Focus();
            return;
        }
        else
        {
            try
            {
                //更新明细表
                for (int i = 0; i < GridView1.Rows.Count; i++)
                {

                    int id = Convert.ToInt32(Request.QueryString["id"]);
                    string cjsl = ((TextBox)GridView1.Rows[i].Cells[11].FindControl("TextBox2")).Text;
                    string beizhu = ((TextBox)GridView1.Rows[i].Cells[12].FindControl("TextBox3")).Text;
                    string sqlstr = "update ranyouxiaohao set duanshuliang='" + cjsl + "'/danjia,jine='" + cjsl + "', beizhu='" + beizhu + "' where id=" + id;
                    //Response.Write(sqlstr);
                    //Response.End();
                    sqlcon = new SqlConnection(strCon);
                    sqlcom = new SqlCommand(sqlstr, sqlcon);
                    sqlcon.Open();
                    sqlcom.ExecuteNonQuery();
                    sqlcon.Close();
                }
                //更新管理表
                string sqlstr2 = "update ranyouxiaohao set duanname='" + Label2.Text + "',duantime='" + DateTime.Now.ToString() + "',duanip='" + Page.Request.UserHostAddress.ToString().Trim() + "',liezhi='" + DropDownList_liezhi.SelectedValue + "',zt=4 where id=" + Request.QueryString["id"];
                //Response.Write(sqlstr2);
                //Response.End();
                sqlcon = new SqlConnection(strCon);
                sqlcom = new SqlCommand(sqlstr2, sqlcon);
                sqlcon.Open();
                sqlcom.ExecuteNonQuery();
                sqlcon.Close();

            }
            catch (SqlException)
            {
                throw;
            }

            string jinggao3 = "<SCRIPT language=javascript>alert('";
            jinggao3 += "审批操作成功！";
            jinggao3 += "');</SCRIPT>";
            ClientScript.RegisterStartupScript(this.GetType(), "aa3", jinggao3);
            ClientScript.RegisterStartupScript(this.GetType(), "aa4", "<SCRIPT language=javascript>document.location.href='duan_shenpilist.aspx';</SCRIPT>");
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#D1DDF1'");
            e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        }
        if (e.Row.RowIndex != -1)
        {
            int id = e.Row.RowIndex + 1;
            e.Row.Cells[0].Text = id.ToString();
            e.Row.Cells[6].Text = e.Row.Cells[6].Text.Replace("&amp;#215;", "×").Replace("&amp;quot;", "");
            SqlDataSource2.SelectCommand = "SELECT * FROM [ranyouxiaohao] WHERE (([nian] = '" + e.Row.Cells[5].Text + "') AND ([chehao] = '" + e.Row.Cells[4].Text + "') and id<>" + e.Row.Cells[13].Text + ") ORDER BY [addtime] DESC";
            GridView2.DataBind();
            GridView2.Caption = e.Row.Cells[4].Text + "  " + e.Row.Cells[5].Text + "年度计划单列表";
        }

    }

}